package com.ricky.spring.demo.config;

/**
 * @author leiruiqi {@literal <leiruiqi@baijiahulian.com>}
 **/

import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class ApollRefreshConfig implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    //@Resource
    //private RefreshScope refreshScope;


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @ApolloConfigChangeListener
    private void refresh(ConfigChangeEvent changeEvent){
        log.info("configChageEvent={}",changeEvent.changedKeys());

        applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
       // refreshScope.refresh("wxKefuConfProperties");

    }
}
